package com.demo.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.demo.adapter.ImageAdapter;
import com.demo.engine.entity.BackData;
import com.demo.executor.HttpConnector;
import com.demo.executor.IExecutor.ExecutorCallBackListener;
import com.demo.model.AIDAXiuModel;
import com.demo.services.ConverServices;
import com.demo.widget.NLongListView;
import com.demo.widget.NLongListView.DataListener;
import com.example.androiddemo.R;

public class LongListViewActivity extends Activity {

	private static final String TAG = "LongListViewActivity";
	
	// Views
	private NLongListView longListView;

	// Data
	private int limit = 15;
	private List<AIDAXiuModel> models = new ArrayList<AIDAXiuModel>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longlistview);

		fetchData();
		initView();
		bindListener();
	}

	private void fetchData() {
		
		final int oldPosition = this.models.size();
		
		HttpConnector connector = ConverServices.showConver("LW3vpHeB0DU=", "301", "0", "1", limit);
		connector.setExecutorCallBackListener(new ExecutorCallBackListener() {
			
			@Override
			public void executorCallback(BackData backData) {
				if(backData.isSuccess()) {
					ArrayList<AIDAXiuModel> newItems = (ArrayList<AIDAXiuModel>) backData.getResultData();

					for (AIDAXiuModel aidaXiuModel : newItems) {
						Log.i(TAG, "ImageUrl:" + aidaXiuModel.getConver());
						models.add(aidaXiuModel);
					}
					longListView.notifyDataSetChanged(oldPosition);
				} else {
					Log.w(TAG, backData.getMsg());
				}
			}
		});
	}

	private void initView() {
		longListView = (NLongListView) findViewById(R.id.longListView);
		ImageAdapter adapter = new ImageAdapter(LongListViewActivity.this, models, R.layout.item_water);
		longListView.setColumnCount(3);
		longListView.setAdapter(adapter);
	}
	
	private void bindListener() {
		
		longListView.setDataListener(new DataListener() {
			
			@Override
			public void getOldData() {
				fetchData();
			}
			
			@Override
			public void getNewData() {
				
			}
		});
	}

}
